package main

import (
	"encoding/base64"
	"fmt"
	"github.com/wcharczuk/go-chart/v2"
	"github.com/wcharczuk/go-chart/v2/drawing"
	"log"
	"os"

	charts "github.com/vicanso/go-charts"
	"io/ioutil"
	"path/filepath"
)

type Render func() ([]byte, error)

func main() {
	fontData, err := ioutil.ReadFile("D:\\code\\go\\go-learn\\go-project\\test-charts\\GB2312.ttf")
	if err != nil {
		log.Fatalln("Error when reading font file:", err)
	}
	if err := charts.InstallFont("chinese", fontData); err != nil {
		log.Fatalln("Error when installing font:", err)
	}

	m := map[string]Render{
		"charts-bar.png": chartsRender,
		//"echarts-bar.png":  echartsRender,
	}
	for name, fn := range m {
		buf, err := fn()
		if err != nil {
			panic(err)
		}
		err = writeFile(name, buf)
		if err != nil {
			panic(err)
		}
	}
}

func chartsRender() ([]byte, error) {
	myStyle := chart.Style{
		FillColor: drawing.Color{
			R: 145,
			G: 204,
			B: 117,
			A: 255,
		},
	}
	d, err := charts.Render(charts.ChartOption{
		SeriesList: []charts.Series{
			{
				Type: charts.ChartTypeBar,
				Data: []charts.SeriesData{
					{Value: 1100},
					{Value: 1100},
					{Value: 1100},
					{Value: 1100},
					{Value: 1100},
					{Value: 1100},
					{Value: 2200, Style: myStyle},
					{Value: 2200, Style: myStyle},
					{Value: 2200, Style: myStyle},
					{Value: 3300, Style: myStyle},
					{Value: 3300, Style: myStyle},
					{Value: 3300, Style: myStyle},
				},
			},
		},
	},
		// output type
		charts.PNGTypeOption(),
		// 中文字体设置
		charts.FontFamilyOptionFunc("chinese"),
		charts.WidthOptionFunc(960),
		charts.HeightOptionFunc(640),
		// title
		charts.TitleOptionFunc(charts.TitleOption{
			Text: "设备运营成本(千元)",
			Left: "40%",
		}),
		charts.LegendOptionFunc(
			charts.LegendOption{
				Top: "40",
				Data: []string{
					"账单成本",
					"预测成本",
				},
				Icon: charts.LegendIconRect,
			},
		),
		// x axis
		charts.XAxisOptionFunc(charts.NewXAxisOption([]string{
			"01月", "02月", "03月", "04月", "05月", "06月",
			"07月", "08月", "09月", "10月", "11月", "12月",
		})),
		charts.YAxisOptionFunc(
			charts.YAxisOption{
				Formatter: "{value}",
			},
		),
	)
	if err != nil {
		return nil, err
	}
	return d.Bytes()
}

func echartsRender() ([]byte, error) {
	return charts.RenderEChartsToPNG(`{
			"title": {
				"text": "用户访问次数",
				"textStyle": {
					"fontFamily": "chinese"
				}
			},
			"xAxis": {
				"type": "category",
				"data": [
					"星期一",
					"Tue",
					"Wed",
					"Thu",
					"Fri",
					"Sat",
					"星期天"
				]
			},
			"yAxis": {
				"type": "value"
			},
			"series": [
				{
					"data": [
						120,
						200,
						150,
						80,
						70,
						110,
						130
					],
					"type": "bar"
				}
			]
		}`)
}

func writeFile(file string, buf []byte) error {
	tmpPath := "./tmp"
	err := os.MkdirAll(tmpPath, 0700)
	if err != nil {
		return err
	}

	file = filepath.Join(tmpPath, file)
	err = os.WriteFile(file, buf, 0600)
	if err != nil {
		return err
	}

	barBase64 := imgToStr(buf)
	fmt.Println("barBase64=", barBase64)

	return nil
}

// 生成base64编码格式, <img src="...">
const imgStrPrefix = "data:image/png;base64,"

func imgToStr(buf []byte) string {
	return imgStrPrefix + base64.StdEncoding.EncodeToString(buf)
}
